<?php 
/*
 * Session variables:
 * $_SESSION['database'] [string] = database name of the current tutorial centre
 * $_SESSION['type'] [string] = user type of the logged in user
 * $_SESSION['authorised'] [boolean] = whether or not the current user is logged in
 * $_SESSION['user'] [string] = the username/ email of the logged in user 
 */
session_start();

if(isset($_SESSION['authorised']) && $_SESSION['authorised'] == true)
{
	if(isset($_SESSION['type']))
	{
		if($_SESSION['type'] == 'administrator')
		{
			header('Location: admin_index.php');
			exit();
		}
		elseif($_SESSION['type'] == 'tutor')
		{
			?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home</title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
<style>
.nav {
	width: 210px;
	height: auto;
	border: thin solid #09C;
	margin-left: 10px;
	text-align: center;
}

.mainline {
	width: 712px;
	height: auto;
	text-align: center;
}
</style>
</head>
<body bgcolor="#FFFFFF">
	<center>

		<div id="wrapper">
			<div id="header">
				<h1></h1>
			</div>

			<div id="header2">
				<h3></h3>
			</div>

			<div id="nav">
				<?php include 'tutor_navigation.php';?>
			</div>

			<div id="mainline">
				<h4>Class Viewing</h4>
				<div id="body">
				<form action=tutor_classlist.php method=POST>	
					<table width="700" align="right" height="auto"  cellpadding="3" cellspacing="1">
						<table width="700" height="auto" cellpadding="3" cellspacing="1" align="center">
        <tr height="40" bgcolor="LightSteelBlue">
            <th>Subject</th>
            <th>Section</th>
			<th>Room / Schedule</th>
			<th>Date</th>
			<th>Action</th>
        </tr>
            <?php
           
            include ('connection.inc');
            $username=$_SESSION['user'];
            $sql = "SELECT teacher_id FROM teachers where username='$username'";
			$result = pg_query($sql);
            $teacher_id = pg_fetch_result($result, 0, 'teacher_id');
            
            $sql = "SELECT * FROM classes where teacher_id=$teacher_id order by class_id";
			$result = pg_query($sql);
            $count = pg_num_rows($result);
            if($count<1)
            {
            	echo "</table></form>";
            	echo "<br>No classes";
            }
            else
            {
            	
	            for($i = 0; $i < $count; $i++)
	            {
	                $class_id = pg_fetch_result($result, $i, 'class_id');
					$class_subj = pg_fetch_result($result, $i, 'subject');
					$section = pg_fetch_result($result, $i, 'section');
					$start_date = pg_fetch_result($result, $i, 'startdate');
	                $time = date_create_from_format('Y-m-d', $start_date);
	                $startdate= date_format($time, 'M-d-Y');
					
					$end_date = pg_fetch_result($result, $i, 'enddate');
					$time = date_create_from_format('Y-m-d', $end_date);
	                $enddate= date_format($time, 'M-d-Y');
					$room_id = pg_fetch_result($result,$i, 'room_id');
					
					$query = "SELECT * FROM rooms where room_id='$room_id'";
           			$qresult= pg_query($query);
           			$capacity = pg_fetch_result($qresult, 0, 'capacity');
           			
					$starttime= pg_fetch_result($result, 'starttime');
					$endtime= pg_fetch_result($result, 'endtime');
					echo "<tr height=\"30\" bgcolor=\"#f5f5f5\">
						<td align=\"center\">$class_subj</td>
	                	<td align=\"center\">$section</td>
						<td align=\"center\">$room_id / $starttime - $endtime</td>
						<td align=\"center\">$startdate to $enddate</td>
	                	<td><input type=submit name=\"action\" value=\"Class List $class_id\"  /></td>";
					echo "</tr>";
	            }
               echo "</table></form>";
            }
        ?>
					</table>
				</div>
			</div>

			<br class="clearfloat"> <br />

			<div id="footer">
				<p align="center">
					Tutorial Business Manager: info@tbms.com <br />Credits/Engineers:
					Aquino, Chloebelle; Dela Cruz, Roy Erwin; Maliwanag, Ranyel Bryan;
					Manzano, Audrey Ann; Ng, Sheila Regine<br /> <br />copyright 2011
				</p>
			</div>
		</div>
	</center>
</body>
</html>
			<?php
		}
		elseif($_SESSION['type'] == 'student')
		{
			header('Location: student_index.php');
			exit();
		}
		elseif($_SESSION['type'] == 'parent')
		{
			header('Location: parent_index.php');
			exit();
		}
	}
}
else
{
	header('Location: index.php');
}


?>